package com.tepth.vehiclemaintenance.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.bean.MaintainListWork;
import com.tepth.vehiclemaintenance.ui.view.statebackground.OneDrawable;

import java.util.List;


/**
 * Created by pc on 2017/3/16.
 */

public class RepairOperatorAdapter extends BaseQuickAdapter<MaintainListWork.ListContentsBean.ListRemarkBean, BaseViewHolder> {

    private Drawable iconAdd;
    private Drawable iconMinus;
    private int mSate;
    private boolean mIsManual;

    public RepairOperatorAdapter(Context context
            , List<MaintainListWork.ListContentsBean.ListRemarkBean> list
            , int sate
            , boolean isManual) {
        super(R.layout.item_repair_operator, list);
        iconAdd = OneDrawable.createBgDrawable(context, R.drawable.ic_add_circle_24dp);
        iconMinus = OneDrawable.createBgDrawable(context, R.drawable.ic_minus_circle_24dp);
        mSate = sate;
        mIsManual = isManual;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MaintainListWork.ListContentsBean.ListRemarkBean item) {

        try {
            final CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    item.setChecked(!item.isChecked());
                    // OkBus.getInstance().onEvent(RepairActivity.OK_BUS_TAG, item);
                }
            });
            checkBox.setChecked(item.isChecked());
            helper.addOnClickListener(R.id.btn_delete);
            helper.setImageDrawable(R.id.iv_add, iconAdd);
            helper.addOnClickListener(R.id.iv_add);
            helper.setImageDrawable(R.id.iv_minus, iconMinus);
            helper.addOnClickListener(R.id.iv_minus);
            helper.setText(R.id.tv_operator, item.getOperation().getName());
            MaintainListWork.ListContentsBean.ListRemarkBean.AccessoryBean part = item.getAccessory() == null ? item.getComponent() : item.getAccessory();
            helper.setText(R.id.tv_part, part.getName());
            helper.setText(R.id.tv_count, item.getUseCount() + "");
            helper.setText(R.id.tv_unit, part.getUnit());
            MaintainListWork.ListContentsBean.ListRemarkBean.WorkPersonBean workPersonBean =
                    item.getWorkPerson();
            View viewDelete = helper.getView(R.id.btn_delete);
            View viewComplete = helper.getView(R.id.iv_complete);

            if(mIsManual){//手动添加
                if(mSate == 1){//已打卡 不能再编辑
                    viewDelete.setVisibility(View.GONE);
                    viewComplete.setVisibility(View.VISIBLE);
                    checkBox.setVisibility(View.INVISIBLE);
                    helper.setVisible(R.id.iv_add,false);
                    helper.setVisible(R.id.iv_minus,false);
                }else{//其他状态可以编辑
                    viewDelete.setVisibility(View.VISIBLE);
                    viewComplete.setVisibility(View.GONE);
                    checkBox.setVisibility(View.VISIBLE);
                    helper.setVisible(R.id.iv_add,true);
                    helper.setVisible(R.id.iv_minus,true);
                }
            }else {//自带的操作项不能删除
                viewDelete.setVisibility(View.GONE);
                if(mSate == 1){//已打卡 不能再编辑
                    viewComplete.setVisibility(View.VISIBLE);
                    checkBox.setVisibility(View.INVISIBLE);
                    helper.setVisible(R.id.iv_add,false);
                    helper.setVisible(R.id.iv_minus,false);
                }else{//未打卡除了删除其他的都能操作
                    viewComplete.setVisibility(View.GONE);
                    checkBox.setVisibility(View.VISIBLE);
                    helper.setVisible(R.id.iv_add,true);
                    helper.setVisible(R.id.iv_minus,true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
